normalize waypoint position. (#1331)
authortsteven4 <13596209+tsteven4@users.noreply.github.com>
Fri, 30 Aug 2024 23:11:09 +0000 (17:11 -0600)
committerGitHub <noreply@github.com>
Fri, 30 Aug 2024 23:11:09 +0000 (17:11 -0600)
defs.h
waypt.cc

diff --git a/defs.h b/defs.h
index d50e49429f06543a0b2197905248598b70ca7162..2380b89e48e7d3014131d700383facb35b36dbf4 100644 (file)
--- a/defs.h
+++ b/defs.h
@@ -357,6 +357,7 @@ public:
   void SetCreationTime(qint64 t, qint64 ms = 0);
   Geocache* AllocGCData();
   int EmptyGCData() const;
+  void NormalizePosition();
   PositionDeg position() const {return PositionDeg(latitude, longitude);}
   void SetPosition(const PositionDeg& pos)
   {
index b2645551183ad0b33b644d56f94f31db2eb1bb5d..191cdc84142a6ad80edf1bca87867440c6740e04 100644 (file)
--- a/waypt.cc
+++ b/waypt.cc
@@ -560,31 +560,53 @@ Waypoint::EmptyGCData() const
   return (gc_data == &Waypoint::empty_gc_data);
 }
 
-void
-WaypointList::waypt_add(Waypoint* wpt)
-{
-  double lat_orig = wpt->latitude;
-  double lon_orig = wpt->longitude;
-  append(wpt);
+void Waypoint::NormalizePosition()
+{
+  double lat_orig = this->latitude;
+  double lon_orig = this->longitude;
+
+  if (this->latitude < -90.0 || this->latitude > 90.0) {
+    bool fliplon = false;
+    this->latitude = remainder(this->latitude, 360.0); // -180 <= this->latitude <= 180
+    if (this->latitude < -90.0) {
+      this->latitude = -180.0 - this->latitude;
+      fliplon = true;
+    } else if (this->latitude > 90.0) {
+      this->latitude = 180.0 - this->latitude;
+      fliplon = true;
+    }
 
-  if (wpt->latitude < -90) {
-    wpt->latitude += 180;
-  } else if (wpt->latitude > +90) {
-    wpt->latitude -= 180;
+    if (fliplon) {
+      if (this->longitude < 0.0) {
+        this->longitude += 180.0;
+      } else {
+        this->longitude -= 180.0;
+      }
+    }
   }
-  if (wpt->longitude < -180) {
-    wpt->longitude += 360;
-  } else if (wpt->longitude > +180) {
-    wpt->longitude -= 360;
+
+  if (this->longitude < -180.0 || this->longitude >= 180.0) {
+    this->longitude = remainder(this->longitude, 360.0); // -180 <= this->longitude <= 180
+    if (this->longitude == 180.0) {
+      this->longitude = -180.0;
+    }
   }
 
-  if ((wpt->latitude < -90) || (wpt->latitude > 90.0))
-    fatal(FatalMsg() << wpt->session->name
+  if ((this->latitude < -90) || (this->latitude > 90.0))
+    fatal(FatalMsg() << this->session->name
           << "Invalid latitude" << lat_orig << "in waypoint"
-          << wpt->shortname);
-  if ((wpt->longitude < -180) || (wpt->longitude > 180.0))
+          << this->shortname);
+  if ((this->longitude < -180) || (this->longitude > 180.0))
     fatal(FatalMsg() << "Invalid longitude" << lon_orig << "in waypoint"
-          << wpt->shortname);
+          << this->shortname);
+}
+
+void
+WaypointList::waypt_add(Waypoint* wpt)
+{
+  append(wpt);
+
+  wpt->NormalizePosition();
 
   /*
    * Some input may not have one or more of these types so we
@@ -621,6 +643,8 @@ WaypointList::add_rte_waypt(int waypt_ct, Waypoint* wpt, bool synth, QStringView
 {
   append(wpt);
 
+  wpt->NormalizePosition();
+
   if (synth && wpt->shortname.isEmpty()) {
     wpt->shortname = QStringLiteral("%1%2").arg(namepart).arg(waypt_ct, number_digits, 10, QChar('0'));
     wpt->wpt_flags.shortname_is_synthetic = 1;